package GUI;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import main.ScreenController;
import GUI.Controller.Controller;

@SuppressWarnings("serial")
public class LogInScreen extends JPanel{
	private static LogInScreen instance = null;
	private ScreenController screen_controller;
	
	private JPanel top_panel;
	private JPanel middle_panel;
	private JPanel bottom_panel;
	
	private JLabel title;
	
	private JLabel username_label;
	private JTextField username_text;
	
	private JLabel password_label;
	private JPasswordField password_text;
	
	private JButton login_button;
	private JButton create_account;
	
	public static LogInScreen getInstance(){
		if(instance == null){
			System.out.println("making singleton!");
			instance = new LogInScreen();
		}
		return instance;
	}
	
	private LogInScreen(){
		this.setVisible(true);
		screen_controller = ScreenController.getInstance();
		//this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLayout(new BorderLayout());
		
		this.SetUpTopPanel();
		this.SetUpMiddlePanel();
		this.SetUpBottomPanel();
		this.setSize(200,150);
	}
	
	public void SetUpTopPanel(){
		top_panel = new JPanel();
		this.add(top_panel,BorderLayout.NORTH);
		title = new JLabel("Welcome to Suite Dreams");
		top_panel.add(title);
	}
	
	public void SetUpMiddlePanel(){
		middle_panel = new JPanel();
		this.add(middle_panel,BorderLayout.CENTER);
		middle_panel.setLayout(new GridLayout(0,2));
		username_label = new JLabel("Username:");
		middle_panel.add(username_label);
		username_text = new JTextField(15);
		username_text.setEditable(true);
		middle_panel.add(username_text);
		password_label = new JLabel("Password:");
		middle_panel.add(password_label);
		password_text = new JPasswordField(15);
		password_text.setEditable(true);
		middle_panel.add(password_text);
	}
	
	public void SetUpBottomPanel(){
		bottom_panel = new JPanel();
		this.add(bottom_panel,BorderLayout.SOUTH);
		bottom_panel.setLayout(new BorderLayout());
		
		login_button = new JButton("Log-In");
		login_button.addActionListener(new LogInButton());
		bottom_panel.add(login_button,BorderLayout.WEST);
		
		create_account = new JButton("Create Account");
		create_account.addActionListener(new CreateAccountButton());
		bottom_panel.add(create_account,BorderLayout.EAST);
	}
	
	private class LogInButton implements ActionListener{

		@SuppressWarnings("deprecation")
		@Override
		public void actionPerformed(ActionEvent arg0) {
			//verify

			LogInScreen login = LogInScreen.getInstance();
			int type = Controller.Login(username_text.getText(), password_text.getText());
			if(type==0){
				screen_controller.InitializeCustomerPanel(Controller.GetCustomerID(username_text.getText(), password_text.getText()));
				screen_controller.ViewCustomerScreen();				
			}else if(type==1){
				screen_controller.InitializeEmployeePanel();
				screen_controller.ViewEmployeeScreen();				
			}else if(type==2){
				screen_controller.InitializeAdminPanel();
				screen_controller.ViewAdminScreen();				
			}else{
				JOptionPane.showMessageDialog(login, "Invalid Username or Password",
					    "Error",
					    JOptionPane.WARNING_MESSAGE);
				return;
			}
			repaint(500);
			//login.dispose();
		}
	}
	
	private class CreateAccountButton implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			//verify
			screen_controller.AddCustomerAccount();
			repaint(500);
		}
	}
}
